# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  def menu
    if logged_in?
      "<ul>
        <li>#{link_to 'Salir', logout_url}</a></li>
        <li>Hola #{current_user.login},#{link_to 'configura tu cuenta', update_url}</a></li>
      </ul>"
    else
    "<ul>
      <li>#{link_to 'Login', login_url}</a></li>
      <li>#{link_to 'Regístrate', signup_url}</a></li>
    </ul>"
    end
  end
  
  def timetowords(date)
    date = date.to_time if date.respond_to?(:to_time)
    to_time = Time.now
    distance_in_minutes = (((to_time - date).abs)/60).round
    distance_in_seconds = ((to_time - date).abs).round

    # 1378 - 1380
    case distance_in_minutes
      when 0..44           then 'hace un rato'
      when 45..89          then 'hace una hora'
      when 90..1498        then 'hace %d horas' % [(distance_in_minutes.to_f / 60.0).round]
      when 1499..2938      then "#{'ayer a las'} #{date.strftime('%H:%M')}"
      when 2939..1051919   then date.strftime("%d/%m/%Y #{'a las'} %H:%M")
      else
    end
  end
  
  def sp_distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round
    distance_in_seconds = ((to_time - from_time).abs).round

    case distance_in_minutes
      when 0..1
        return (distance_in_minutes == 0) ? 'menos de un minuto' : '1 minuto' unless include_seconds
      case distance_in_seconds
        when 0..4   then 'menos de 5 segundos'
        when 5..9   then 'menos de 10 segundos'
        when 10..19 then 'menos de 20 segundos'
        when 20..39 then 'medio minuto'
        when 40..59 then 'menos de un minuto'
      else             '1 minuto'
    end

    when 2..44           then "#{distance_in_minutes} minutos"
    when 45..89          then '1 hora'
    when 90..1439        then "#{(distance_in_minutes.to_f / 60.0).round} horas"
    when 1440..2879      then '1 día'
    when 2880..43199     then "#{(distance_in_minutes / 1440).round} días"
    when 43200..86399    then '1 mes'
    when 86400..525599   then "#{(distance_in_minutes / 43200).round} meses"
    when 525600..1051199 then '1 año'
    else                      "#{(distance_in_minutes / 525600).round} años"
    end
  end
  
end
